package com.lcxw.hxzb.friendcircle.project.system.controller;

import java.math.BigDecimal;
import java.sql.Date;
import java.util.List;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.lcxw.hxzb.friendcircle.common.utils.DateUtils;
import com.lcxw.hxzb.friendcircle.framework.web.controller.BaseController;
import com.lcxw.hxzb.friendcircle.framework.web.domain.AjaxResult;
import com.lcxw.hxzb.friendcircle.framework.web.page.TableDataInfo;
import com.lcxw.hxzb.friendcircle.project.system.domain.YwpdLoves;
import com.lcxw.hxzb.friendcircle.project.system.domain.YwpdLovesMoneyDetail;
import com.lcxw.hxzb.friendcircle.project.system.service.IYwpdLovesMoneyDetailService;
import com.lcxw.hxzb.friendcircle.project.system.service.IYwpdLovesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.crypto.Data;

/**
 * 资产值明细Controller
 *
 * @author ruoyi
 * @date 2020-09-27
 */
@RestController
@RequestMapping("/system/lovesMoneyDetail")
public class YwpdLovesMoneyDetailController extends BaseController {
    @Autowired
    private IYwpdLovesMoneyDetailService ywpdLovesMoneyDetailService;
    @Autowired
    private IYwpdLovesService ywpdLovesService;

    /**
     * 查询资产值明细列表
     */
    @GetMapping("/list")
    public TableDataInfo list(YwpdLovesMoneyDetail ywpdLovesMoneyDetail) {
        startPage();
        List<YwpdLovesMoneyDetail> list = ywpdLovesMoneyDetailService.selectYwpdLovesMoneyDetailList(ywpdLovesMoneyDetail);
        return getDataTable(list);
    }


    /**
     * 获取资产值明细详细信息
     */
    @GetMapping(value = "/{detailId}")
    public AjaxResult getInfo(@PathVariable("detailId") Long detailId) {
        return AjaxResult.success(ywpdLovesMoneyDetailService.selectYwpdLovesMoneyDetailById(detailId));
    }

    /**
     * 打卡  起床 Get up and punch in
     */
    @PostMapping("/getUpAndPunchIn")
    public AjaxResult getUpAndPunchIn(@RequestBody YwpdLovesMoneyDetail ywpdLovesMoneyDetail) {
        String dateNow = DateUtils.getDate();
        String start = dateNow + " 06:00:00";
        String end = dateNow + " 10:00:00";
        DateTime s = DateUtil.parse(start);
        DateTime e = DateUtil.parse(end);
        YwpdLovesMoneyDetail detail = new YwpdLovesMoneyDetail();
        detail.setStart(s);
        detail.setEnd(e);
        List<YwpdLovesMoneyDetail> detailList = ywpdLovesMoneyDetailService.selectMoneyDetailList(detail);
        if (detailList.size() == 0) {
            long startTime = s.getTime();
            long endTime = e.getTime();
            long currentTimeMillis = System.currentTimeMillis();
            if (currentTimeMillis >= startTime && currentTimeMillis <= endTime) {
                ywpdLovesMoneyDetail.setType("1");
                BigDecimal money = BigDecimal.valueOf(200);
                ywpdLovesMoneyDetail.setMoney(money);
                ywpdLovesMoneyDetail.setRemark("起床打卡");
                YwpdLoves ywpdLoves = ywpdLovesService.selectYwpdLovesById(ywpdLovesMoneyDetail.getLovesId());
                ywpdLoves.setMoney(ywpdLoves.getMoney().add(money));
                ywpdLovesService.updateYwpdLoves(ywpdLoves);
                ywpdLovesMoneyDetailService.insertYwpdLovesMoneyDetail(ywpdLovesMoneyDetail);
                return AjaxResult.success("打卡成功！");
            }
            return AjaxResult.error("现在不是打卡时间！");
        }
        return AjaxResult.error("已打卡！");
    }


    /**
     * 打卡  睡觉 Clock in
     */
    @PostMapping("/clockIn")
    public AjaxResult clockIn(@RequestBody YwpdLovesMoneyDetail ywpdLovesMoneyDetail) {
        String dateNow = DateUtils.getDate();
        String start = dateNow + " 20:00:00";
        String end = dateNow + " 23:59:59";
        DateTime s = DateUtil.parse(start);
        DateTime e = DateUtil.parse(end);
        YwpdLovesMoneyDetail detail = new YwpdLovesMoneyDetail();
        detail.setStart(s);
        detail.setEnd(e);
        List<YwpdLovesMoneyDetail> detailList = ywpdLovesMoneyDetailService.selectMoneyDetailList(detail);
        if (detailList.size() == 0) {
            long startTime = s.getTime();
            long endTime = e.getTime();
            long currentTimeMillis = System.currentTimeMillis();
            if (currentTimeMillis >= startTime && currentTimeMillis <= endTime) {
                ywpdLovesMoneyDetail.setType("4");
                BigDecimal money = BigDecimal.valueOf(200);
                ywpdLovesMoneyDetail.setMoney(money);
                ywpdLovesMoneyDetail.setRemark("睡觉打卡");
                YwpdLoves ywpdLoves = ywpdLovesService.selectYwpdLovesById(ywpdLovesMoneyDetail.getLovesId());
                ywpdLoves.setMoney(ywpdLoves.getMoney().add(money));
                ywpdLovesService.updateYwpdLoves(ywpdLoves);
                ywpdLovesMoneyDetailService.insertYwpdLovesMoneyDetail(ywpdLovesMoneyDetail);
                return AjaxResult.success("打卡成功！");
            }
            return AjaxResult.error("现在不是打卡时间！");
        }
        return AjaxResult.error("已打卡！");
    }

    /**
     * 修改资产值明细
     */
    @PutMapping
    public AjaxResult edit(@RequestBody YwpdLovesMoneyDetail ywpdLovesMoneyDetail) {
        return toAjax(ywpdLovesMoneyDetailService.updateYwpdLovesMoneyDetail(ywpdLovesMoneyDetail));
    }

    /**
     * 删除资产值明细
     */
    @DeleteMapping("/{detailIds}")
    public AjaxResult remove(@PathVariable Long[] detailIds) {
        return toAjax(ywpdLovesMoneyDetailService.deleteYwpdLovesMoneyDetailByIds(detailIds));
    }
}
